home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / tools / weave.c < prev   
C/C++ Source or Header  |  1994-08-01  |  7KB  |  336 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    weave -
  19.  *        A weaving simulator for IRIS workstations.  This simulates
  20.  *    a loom and the pattern created with different settings of the headles
  21.  *    and various treadling patterns.  The tieups are also programable.
  22.  *    Click the left mouse over the left area to change the treadling
  23.  *    pattern, Click the left mouse over the top area to change the 
  24.  *    threading of the headles.  Click the left mouse over the box in
  25.  *    the upper left corner to alter the tieups.
  26.  *
  27.  *            Paul Haeberli and Amie Slate - 1991
  28.  */
  29. #include "stdio.h"
  30. #include "gl.h"
  31. #include "device.h"
  32.  
  33. /* feel free to change the number of treadles and headles */
  34.  
  35. #define NTREADLES    (4)
  36. #define NHEADLES    (4)
  37.  
  38. #define BOXSIZE        (8)
  39.  
  40. #define TOTX        (800/BOXSIZE)
  41. #define TOTY        (600/BOXSIZE)
  42. #define NX        (TOTX-NTREADLES)
  43. #define NY        (TOTY-NHEADLES)
  44.  
  45. int up[NX];
  46. int headles[NX];
  47. int treadles[NY];
  48. int tieup[NHEADLES][NTREADLES];
  49.  
  50. main()
  51. {
  52.     short val;
  53.     int x, y, menu;
  54.  
  55.     prefsize(TOTX*BOXSIZE,TOTY*BOXSIZE);
  56.     winopen("weave");
  57.     qmouse();
  58.     subpixel(1);
  59.     pseudorgb();
  60.     initstate();
  61.     makeframe();
  62.     menu = defpup("weave %t|init state|print help");
  63.     while(1) {
  64.     switch(qread(&val)) {
  65.         case REDRAW:
  66.         makeframe();
  67.         break;
  68.         case LEFTMOUSE:
  69.         if(val) {
  70.             x = getmousex()/BOXSIZE;
  71.             y = getmousey()/BOXSIZE;
  72.             if(x<NTREADLES) {
  73.             if(y>=NY) {
  74.                 y = y-NY;
  75.                 tieup[y][x] = 1-tieup[y][x];
  76.                 makeframe();
  77.             } else {
  78.                 treadles[y] = x;
  79.                 updaterow(y);
  80.             }
  81.             } else if(y>=NY) {
  82.             x = x-NTREADLES;
  83.             headles[x] = y-NY;
  84.             updatecol(x);
  85.             }
  86.         }
  87.         break;
  88.         case MENUBUTTON:
  89.         if(val) {
  90.             switch(dopup(menu)) {
  91.             case 1:
  92.                 initstate();
  93.                 makeframe();
  94.                 break;
  95.             case 2:
  96.                 printhelp();
  97.                 break;
  98.             }
  99.         }
  100.     }
  101.     }
  102. }
  103.  
  104. initstate()
  105. {
  106.     int i, j;
  107.  
  108.     for(i=0; i<NX; i++)
  109.     headles[i] = i%NHEADLES;
  110.     for(i=0; i<NY; i++)
  111.     treadles[i] = i%NTREADLES;
  112.     for(i=0; i<NHEADLES; i++) {
  113.     for(j=0; j<NTREADLES; j++) {
  114.         if(i == j || i == ((j+1)%NTREADLES))
  115.         tieup[i][j] = 1;
  116.         else
  117.         tieup[i][j] = 0;
  118.     }
  119.     }
  120. }
  121.  
  122. #define DEL (1*1.0/BOXSIZE)
  123.  
  124. makeframe()
  125. {
  126.     int x, y, t, ty;
  127.  
  128.     reshapeviewport();
  129.     ortho2(0.0,(float)TOTX,0.0,(float)TOTY);
  130.     panelcolor();
  131.     clear();
  132.  
  133. /* draw the contents of the headles */
  134.     pushmatrix();
  135.     translate((float)NTREADLES,(float)NY,0.0);
  136.     markcolor();
  137.     for(x=0; x<NX; x++) {
  138.     fillrecti(x,headles[x],x+1,headles[x]+1);
  139.     }
  140.     popmatrix();
  141.  
  142. /* draw the contents of the treadles */
  143.     markcolor();
  144.     for(y=0; y<NY; y++) {
  145.     fillrecti(treadles[y],y,treadles[y]+1,y+1);
  146.     }
  147.  
  148. /* draw the contents of the tieups */
  149.     pushmatrix();
  150.     translate(0.0,(float)NY,0.0);
  151.     markcolor();
  152.     for(y=0; y<NHEADLES; y++) {
  153.     for(x=0; x<NTREADLES; x++) {
  154.         if(tieup[y][x])
  155.         fillrecti(x,y,x+1,y+1);
  156.     }
  157.     }
  158.     popmatrix();
  159.  
  160. /* draw the pattern */
  161.     pushmatrix();
  162.     translate((float)NTREADLES,0.0,0.0);
  163.     backcolor();
  164.     fillrecti(0,0,NX,NY);
  165.     darkthreadcolor();
  166.     for(x=0; x<NX; x++)
  167.     fillrect(x+DEL,0.0,x+1-DEL,(float)NY);
  168.     lightthreadcolor();
  169.     for(y=0; y<NY; y++)
  170.     fillrect(0.0,y+DEL,(float)NX,y+1-DEL);
  171.     for(y=0; y<NY; y++) {
  172.     for(x=0; x<NX; x++) 
  173.         up[x] = 0;
  174.     t = treadles[y];
  175.     for(ty=0; ty<NHEADLES; ty++) {
  176.         if(tieup[ty][t]) {
  177.         for(x=0; x<NX; x++) {
  178.             if(headles[x] == ty)
  179.             up[x] = 1;
  180.         }
  181.         }
  182.     }
  183.     darkthreadcolor();
  184.     for(x=0; x<NX; x++) {
  185.         if(up[x]) 
  186.         fillrect(x+DEL,y+DEL,x+1-DEL,y+1-DEL);
  187.     }
  188.     }
  189.     popmatrix();
  190.     drawgrid();
  191. }
  192.  
  193. drawgrid()
  194. {
  195.     int x, y;
  196.  
  197. /* draw grid */
  198.     gridcolor();
  199.     for(x=0; x<TOTX; x++) {
  200.     if(x<NTREADLES) {
  201.         move2i(x,0);
  202.         draw2i(x,TOTY);
  203.     } else {
  204.         move2i(x,NY);
  205.         draw2i(x,TOTY);
  206.     }
  207.     }
  208.     for(y=0; y<TOTY; y++) {
  209.     if(y<NY) {
  210.         move2i(0,y);
  211.         draw2i(NTREADLES,y);
  212.     } else {
  213.         move2i(0,y);
  214.         draw2i(TOTX,y);
  215.     }
  216.     }
  217.  
  218. /* draw dark lines */
  219.     blackcolor();
  220.     move2i(NTREADLES,0);
  221.     draw2i(NTREADLES,TOTY);
  222.     move2i(0,NY);
  223.     draw2i(TOTX,NY);
  224. }
  225.  
  226. updaterow(y)
  227. int y;
  228. {
  229.     int x, t, ty;
  230.  
  231.     pushmatrix();
  232.     translate((float)NTREADLES,0.0,0.0);
  233.     lightthreadcolor();
  234.     fillrect(0.0,y+DEL,(float)NX,y+1-DEL);
  235.     for(x=0; x<NX; x++) 
  236.     up[x] = 0;
  237.     t = treadles[y];
  238.     for(ty=0; ty<NHEADLES; ty++) {
  239.     if(tieup[ty][t]) {
  240.         for(x=0; x<NX; x++) {
  241.         if(headles[x] == ty)
  242.             up[x] = 1;
  243.         }
  244.     }
  245.     }
  246.     darkthreadcolor();
  247.     for(x=0; x<NX; x++) {
  248.     if(up[x]) 
  249.         fillrect(x+DEL,y+DEL,x+1-DEL,y+1-DEL);
  250.     }
  251.     popmatrix();
  252.     panelcolor();
  253.     fillrecti(0,y,NTREADLES,y+1);
  254.     markcolor();
  255.     fillrecti(treadles[y],y,treadles[y]+1,y+1);
  256.     drawgrid();
  257. }
  258.  
  259. updatecol(x)
  260. int x;
  261. {
  262.     int y, t, ty;
  263.  
  264.     pushmatrix();
  265.     translate((float)NTREADLES,0.0,0.0);
  266.     darkthreadcolor();
  267.     fillrect(x+DEL,0.0,x+1-DEL,(float)NY);
  268.     for(y=0; y<NY; y++) {
  269.     t = treadles[y];
  270.     up[0] = 0;
  271.     for(ty=0; ty<NHEADLES; ty++) {
  272.         if(tieup[ty][t]) {
  273.         if(headles[x] == ty)
  274.             up[0] = 1;
  275.         }
  276.     }
  277.     lightthreadcolor();
  278.     if(!up[0]) 
  279.         fillrect(x+DEL,y+DEL,x+1-DEL,y+1-DEL);
  280.     }
  281.     panelcolor();
  282.     fillrecti(x,NY,x+1,NY+NHEADLES);
  283.     markcolor();
  284.     fillrecti(x,NY+headles[x],x+1,NY+headles[x]+1);
  285.     popmatrix();
  286.     drawgrid();
  287. }
  288.  
  289.  
  290. panelcolor()
  291. {
  292.     rgb(0.8,0.8,0.8);
  293. }
  294.  
  295. backcolor()
  296. {
  297.     rgb(0.25,0.25,0.25);
  298. }
  299.  
  300. lightthreadcolor()
  301. {
  302.     rgb(0.9,0.9,0.9);
  303. }
  304.  
  305. darkthreadcolor()
  306. {
  307.     rgb(0.0,0.0,0.0);
  308. }
  309.  
  310. blackcolor()
  311. {
  312.     rgb(0.0,0.0,0.0);
  313. }
  314.  
  315. markcolor()
  316. {
  317.     rgb(0.5,0.0,0.0);
  318. }
  319.  
  320. gridcolor()
  321. {
  322.     rgb(0.4,0.4,0.4);
  323. }
  324.  
  325. printhelp()
  326. {
  327.     printf("\n\nPaul Haeberli and Amie Slate's weaving simulator:\n\n");
  328.     printf("The state of the headles is shown across the top.  This can be\n");
  329.     printf("changed by clicking with left mouse button to select which\n");
  330.     printf("headle the threads go through.\n\n");
  331.     printf("The treadling pattern can be changed by clicking the left\n");
  332.     printf("mouse over the area on the left hand part of the window\n\n");
  333.     printf("The state of the tieup can be changed by clicking the left\n");
  334.     printf("mouse on the area in the upper left corner\n\n");
  335. }
  336.